//
//  SkillHandle.m
//  BasketballClub
//
//  Created by 孙鸿吉 on 16/1/6.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "SkillHandle.h"
#import <AFNetworking.h>
#import "SkillModel.h"
#import "CoreDataManager.h"
static SkillHandle *skillHandle = nil;
@interface SkillHandle()
//@property (nonatomic,strong)NSMutableArray *array;
@end
@implementation SkillHandle
+ (SkillHandle *)shareSkillHandle
{
    return [[self alloc]init];
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (skillHandle == nil) {
            skillHandle = [super allocWithZone:zone];
        }
    });
    return skillHandle;
}

- (void)loadDataWithNumber:(NSInteger)number withBlock:(SkillBlock)block;
{

    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *urlStr = [NSString stringWithFormat:@"http://api.basketball.app887.com/api/Articles.action?keyword=&npc=%ld&opc=20&type=技巧百科&uid=4467",number];
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [sessionManager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dic1 = dic[@"root"];
        NSArray *array = dic1[@"list"];
        if (self.skillDataArray == nil) {
            self.skillDataArray = [NSMutableArray array];
        }
        for (NSDictionary *dic in array) {
            SkillModel *model = [[SkillModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.skillDataArray addObject:model];
            //存储为本地缓存
            if ([[CoreDataManager shareCoreDateManager] selectAllSkillModel].count > self.skillDataArray.count) {
                [[CoreDataManager shareCoreDateManager] deleteSkillAllData];
            }else{
                [[CoreDataManager shareCoreDateManager] insertSkillDataToSql:model];
            }

        }
        
        dispatch_async(dispatch_get_main_queue(),^{
            block(self.skillDataArray.count > 0);
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求数据失败%@",error);
        //请求失败的时候 加载本地缓存
        self.skillDataArray = [NSMutableArray arrayWithArray:[[CoreDataManager shareCoreDateManager] selectAllSkillModel]];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            block(self.skillDataArray.count > 0);
        });
    }];
    
}


- (void)loadDataWithWebView:(NSString *)number withBlock:(SkillBlock)block
{
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *strUrl = [NSString stringWithFormat:@"http://api.basketball.app887.com/api/GetHtmlCode.action?id=%@",number];
    [sessionManager GET:strUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dic1 = dic[@"root"];
        NSDictionary *dic2 = dic1[@"content"];
        self.WebTitle = dic2[@"title"];
//        NSLog(@"%@",self.WebTitle);
        dispatch_async(dispatch_get_main_queue(),^{
            block(self.WebTitle.length > 0);
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

















@end
